/**
 * @module 基础类和通用的混入
 */

import { BeforeInsert, BeforeUpdate, Column, PrimaryGeneratedColumn } from 'typeorm'
import dayjs from 'dayjs'
import { SoftDeleteEntity } from '../SoftDeleteEntity'
import { DateTimestampColumnTransformer } from '../transform/DateTimestampColumnTransformer'

// 支持主键自增长ID和创建/更新时间的基础实体类
export abstract class CommonEntity extends SoftDeleteEntity {
  @PrimaryGeneratedColumn('increment', { comment: '主键ID' })
  id: number

  @Column('timestamp', { comment: '创建时间', transformer: new DateTimestampColumnTransformer() })
  created_at: string

  @Column('timestamp', { comment: '更新时间', transformer: new DateTimestampColumnTransformer() })
  updated_at: string

  @BeforeInsert()
  updateTimestampColumns() {
    this.created_at = dayjs().format('YYYY-MM-DD HH:mm:ss')
    this.updated_at = dayjs().format('YYYY-MM-DD HH:mm:ss')
  }

  @BeforeUpdate()
  updateUpdatedAt() {
    this.updated_at = dayjs().format('YYYY-MM-DD HH:mm:ss')
  }
}
